package code.util;

import java.security.MessageDigest;

/**
 * Created by IntelliJ IDEA.
 * User: cheny
 * Date: 2010-2-13
 * Time: 12:45:47
 */
public class EncryptionUtil {
    public static String md5(String str) {
        StringBuffer sb = new StringBuffer("");
        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(str.getBytes());
            byte b[] = m.digest();

            for (int i = 0; i < b.length; i++) {
                sb.append(Integer.toHexString((0x000000FF & b[i]) | 0xFFFFFF00).substring(6));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    public static String sha(String str) {
        StringBuffer sb = new StringBuffer("");
        try {
            MessageDigest m = MessageDigest.getInstance("SHA");
            m.update(str.getBytes());
            byte b[] = m.digest();

            for (int i = 0; i < b.length; i++) {
                sb.append(Integer.toHexString((0x000000FF & b[i]) | 0xFFFFFF00).substring(6));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
